#[derive(Debug)]
enum Event {
    //创建时间的三个变体
    Update,
    Delete,
    Unknow,
}

type Message = String;

/// 处理单行事件信息，转换为半结构化数据
fn parse_log(line: &str) -> (Event, Message) {
    let parts: Vec<_> = line //要求Rust推断包含的类型
        .splitn(2, ' ')
        .collect(); //消费一个从line.splitn()产生的迭代器，并返回vec<t>

    if parts.len() == 1 {
        return (Event::Unknow, String::from(line)); //如果不能拆分为两部分，就返回一个错误
    }

    let event = parts[0];
    let rest = String::from(parts[1]);

    match event {
        "UPDATE" | "update" => (Event::Update, rest),
        "DELETE" | "delete" => (Event::Delete, rest),
        _ => (Event::Unknow, String::from(line)), //不能识别就返回整个行
    }
}

fn main() {
    let log = "BEGIN Transction XK342
UPDATE 234:LS/32231 {\"price\": 31.00} -> {\"price\": 40.00}
DELETE 342:LO/22111";
    for line in log.lines() {
        let parse_result = parse_log(line);
        println!("{:?}", parse_result);
    }
}
